import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yt.entity.StuinfoEntity;
import com.yt.util.DBConnection;

public class StuinfoDAO {

	private static Connection conn=null;
	private static PreparedStatement pstm = null;
	private static ResultSet rs = null;

	/**************************** 查询 **********************************/
	public List<StuinfoEntity> select() {
		List<StuinfoEntity> stulist = new ArrayList<StuinfoEntity>();
		try {
			conn = DBConnection.getconn();
			String sql = "select * from stuinfo";
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			while (rs.next()) {
				
				StuinfoEntity stu = new StuinfoEntity();
				
				stu.setStuid(rs.getInt("stuid"));
				stu.setStuname(rs.getString("stuname"));
				stu.setStusex(rs.getString("stusex"));
				stu.setStuage(rs.getInt("stuage"));				
				
				stulist.add(stu);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.finall(rs, pstm, conn);
		}

		return stulist;
	}
	/**************************** 通用 **********************************/
	public int besc(String sql,Object [] obj ) {
		int count=0;		
		try {
			conn=DBConnection.getconn();
			pstm=conn.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				pstm.setObject(i+1, obj[i]);
			}
			count=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.finall(rs, pstm, conn);
		}		
		return count;	
	}	
		
	/**************************** 新增 **********************************/
	public int insert(StuinfoEntity stu) {
		String sql="insert into stuinfo values(?,?,?)";
		Object [] obj={stu.getStuname(),stu.getStusex(),stu.getStuage()};
		return besc(sql, obj);
	}

	/**************************** 删除 **********************************/
	public int delete(int stuid) {
		String sql="delete from stuinfo where stuid=?";
		Object [] obj={stuid};
		return besc(sql, obj);
	}
	/**************************** 修改 **********************************/
	public int update(StuinfoEntity stu) {
		String sql="update stuinfo set stuname=?,stusex=?,stuage=? where stuid=?";
		Object [] obj={stu.getStuname(),stu.getStusex(),stu.getStuage(),stu.getStuid()};
		return besc(sql, obj);
	}
	/**************************** 查询单个 **********************************/
	public StuinfoEntity selectone(int stuid) {	
		StuinfoEntity stu = new StuinfoEntity();	
		try {
			conn = DBConnection.getconn();
			String sql = "select * from stuinfo where stuid=?";
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, stuid);
			rs = pstm.executeQuery();
			while (rs.next()) {										
				stu.setStuid(rs.getInt("stuid"));
				stu.setStuname(rs.getString("stuname"));
				stu.setStusex(rs.getString("stusex"));
				stu.setStuage(rs.getInt("stuage"));				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.finall(rs, pstm, conn);
		}
		return stu;
	}
}